<?php  
## include required files
/*******************************/
require_once '../model/country.php';
require_once '../model/leads.php';
require_once '../model/moduleuser.php';
require_once '../model/state.php';
require_once '../model/membershipplan.php';
include_once '../model/budget.php';
## Paginaton class
require_once ("../includes/classes/pagination_class.php");
/*******************************/
## Create Objects
/*******************************/
$countryObj = new Model_Country();
$leadsObj = new Model_Leads();
$moduleObj=new Model_ModuleUser();
$stateObj = new Model_State();
$objPlan = new Model_PLAN();
$budgetObj = new Model_Budget();
/*******************************/

## Active, Inactive selected records
/*******************************/
if(isset($_POST['go']) != '' && isset($_POST['action']) != '') {
		$ids = implode(",", $_POST['checkall']);
		## check which action is selected
		$hidden_page = $_POST['hidden_pageno'];	
		
		## Delete selected records
		$leadsObj->deleteMultipleLeads($ids);
		$_SESSION['msg'] = "<div class='success_msg'><span>Lead(s) deleted successfully</span></div>";
		header('location:'.SITE_URL.'/admin/home.php?q=leads&page='.$hidden_page);
		exit;
		## Delete code will be here if required
		
} // if close



if(isset($_GET['action']) == 'delete' && isset($_GET['id']) != '') {
	$id = base64_decode($_GET['id']);
	## Delete Leads category
	$leadsObj->deleteLeadsById($id);
	$_SESSION['msg'] = "<div class='success_msg'><span>Lead deleted successfully</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=leads&page='.$_GET['page']);
	exit;
}
/*******************************/



if($_GET['q']=='leads')
{
	$moduelArray=$moduleObj->getModuleHelpText(12);
	$smarty->assign('moduelArray', $moduelArray);
}
##Fetch all leads pages from database
## Get search parameters in variables - 
if($_POST['search'] != '' && $_POST['search'] != 'Region Name') {
	$searchindex =addslashes($_POST['search']);
} else if($_GET['search'] != '') {
	$searchindex = addslashes($_GET['search']);
} else {
	$searchindex = '';
}
$searchindex=trim($searchindex);
// Assign search variable to show in search textbox
$smarty->assign('search',stripslashes (stripslashes ($searchindex)));


if($_GET['col1'] != '') 
{
	$col1 = base64_decode($_GET['col1']);
} 
else if($_GET['col2'] != '') 
{
	$col2 = base64_decode($_GET['col2']);
} 
else if($_GET['col3'] != '') 
{
	$col3 = base64_decode($_GET['col3']);
} 
else 
{
	$col1 = '0';	
}

//echo '<pre>';print_r($_POST);exit;
##Fetch all Leads pages from database

##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	$rowsPerPage = ROW_PER_PAGE;
	if($_GET['col1'] != '') 
	{
		$pageName = "home.php?q=leads&col1=".$_GET['col1'].""; ;
	} 
	else if($_GET['col2'] != '') 
	{
		$pageName = "home.php?q=leads&col2=".$_GET['col2'].""; ;
	} 
	else if($_GET['col3'] != '') 
	{
		$pageName = "home.php?q=leads&col3=".$_GET['col3'].""; ;
	} 
	else 
	{
		$pageName = "home.php?q=leads"; 
	}
	
	## Count all the records
	$leadsArray = $leadsObj->getAllLeads($searchindex,$col1,$col2,$col3);
	$total_rows = count($leadsArray);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##

##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid', $newid);
	$leadsArray = $leadsObj->getAllLeads($searchindex,$col1,$col2,$col3,$rowsPerPage,$offset);
	
	for($i=0;$i<count($leadsArray);$i++)
	{
	/*	$explodeArray = @explode("-",$leadsArray[$i]['budget']);

		$leadsArray[$i]['budgetname']= "$".$explodeArray[0]." To $".$explodeArray[1];	*/
		if($leadsArray[$i]['suburb_id']!='')
		{
			$suburbdetail = $stateObj->getPostalDetailsById($leadsArray[$i]['suburb_id']);		
			$leadsArray[$i]['suburbname']=$suburbdetail['suburb'];
		}
		
		if($leadsArray[$i]['plan_id']!='')
		{
			$getplandetail = $objPlan->getPlanDetailsById($leadsArray[$i]['plan_id']);
			$leadsArray[$i]['plan_name']=$getplandetail['name'];
		}
		
		if($leadsArray[$i]['budget']!='')
		{
			$budgetdetail = $budgetObj->getBudgetDetailsById($leadsArray[$i]['budget']);
			$leadsArray[$i]['budget_name']=$budgetdetail['budget_name'];
		}

	}
	$smarty->assign('leadsArray', $leadsArray);
	
	if($searchindex != '') {
		$other_id = "search=".$searchindex;
	} else {
		$other_id = '';
	}	
	
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	

## Assign session message to smarty variable and unset session variable
if($_SESSION['msg'] != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'leads');
$smarty->assign('mainmenu', '2');

## Unset all the objects created which are on this page
unset($leadsObj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'manageleads/leads.tpl');
unset($smarty);
?>